package com.hxkcloud.patient.dborm;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.table.TableUtils;
import com.hxkcloud.patient.beans.HospitalStatusBean;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by william_sun on 2016-07-22.
 * Description:
 */
public class HospitalStatusDao {
    private Context context;
    private Dao<HospitalStatusBean, Integer> hospitalStatusDao;

    public HospitalStatusDao(Context context) {
        this.context = context;
        try {
            hospitalStatusDao = DatabaseHelper.getHelper(context).getDao(HospitalStatusBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<HospitalStatusBean> getHospitalStatus() {
        try {
            return hospitalStatusDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<HospitalStatusBean> getHospitalStatus(boolean isChanke) {
        try {
            String isChankeS = "0";
            if (isChanke) {
                isChankeS = "1";
                return hospitalStatusDao.queryForAll();
            } else {
                QueryBuilder builder = hospitalStatusDao.queryBuilder();
                builder.where().eq("isChanke", isChankeS);
                List<HospitalStatusBean> statuss = builder.query();
                return statuss;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setHospitalStatus(List<HospitalStatusBean> hs) {
        try {
            clearHospitalStatus();
            for (HospitalStatusBean h : hs) {
                hospitalStatusDao.create(h);
            }
        } catch (SQLException e) {
        }
    }

    public void clearHospitalStatus() {
        try {
            TableUtils.clearTable(hospitalStatusDao.getConnectionSource(), HospitalStatusBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
